package com.daoliuhe.scaffold.service.impl;

import com.daoliuhe.scaffold.mapper.UserGroupMapper;
import com.daoliuhe.scaffold.mapper.UserGroupRoleMapper;
import com.daoliuhe.scaffold.model.UserGroupRole;
import com.daoliuhe.scaffold.service.UserGroupRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

@Service
public class UserGroupRoleServiceImpl implements UserGroupRoleService {
	/***
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(UserGroupRoleServiceImpl.class);

	@Autowired
	UserGroupRoleMapper userGroupRoleMapper;
	
	@Autowired
	UserGroupMapper userGroupMapper;

	@Override
	public Object getRolePageData(UserGroupRole relation) {
		logger.info("getRolePageData,relation:{}",relation);
		Map<String, Object> map = new HashMap<String, Object>();
		int total = userGroupRoleMapper.getRolePageCount(relation);
		map.put("total", total);
		int curPage = relation.getPage();
		int rows = relation.getRows();
		int maxPage = (total + rows - 1) / rows;
		if (curPage > maxPage && maxPage > 0) {
			relation.setPage(maxPage);
		}
		map.put("rows", userGroupRoleMapper.getRolePageData(relation));
		return map;
	}
	
	@Override
	public Object getUserGroupPageData(UserGroupRole relation) {
		logger.info("getUserPageData,relation:{}",relation);
		Map<String, Object> map = new HashMap<String, Object>();
		int total = userGroupRoleMapper.getUserGroupPageCount(relation);
		int curPage = relation.getPage();
		int rows = relation.getRows();
		int maxPage = (total + rows - 1) / rows;
		if (curPage > maxPage && maxPage > 0) {
			relation.setPage(maxPage);
		}
		map.put("total", total);
		map.put("rows", userGroupRoleMapper.getUserGroupPageData(relation));
		return map;
	}
	
	@Override
	public Object saveRelation(UserGroupRole relation) {
		logger.info("saveRelation,relation:{}",relation);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		// 用户id和角色id联合查询
		if (StringUtils.isEmpty(relation.getUserGroupId()) ||
				StringUtils.isEmpty(relation.getRoleId())) {
			reason = "用户或角色不能为空!";
		} else if (userGroupRoleMapper.select(relation).isEmpty()) {
			int result = userGroupRoleMapper.insert(relation);
			if (result == 1) {
				success = true;
			} else {
				reason = "保存失败!";
			}
		} else {
			reason = "已替换!";
		}
		json.put("success", success);
		json.put("reason", reason);
		return json;
	}
	
	@Override
	public Object deleteForUserGroup(String userId, String roleIds) {
		logger.info("deleteForUser,userId:{},roleIds:{}",userId,roleIds);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(roleIds)) {
			reason = "参数错误!";
		} else {
			List<String> list = Arrays.asList(roleIds.split(","));
			userGroupRoleMapper.deleteRelationForUserGroup(userId, list);
			success = true;
		}
		
		json.put("success", success);
		json.put("reason", reason);
		return json;
	}
	
	@Override
	public Object deleteForRole(String userIds, String roleId) {
		logger.info("deleteForRole,userIds:{},roleId:{}",userIds,roleId);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		if (StringUtils.isEmpty(userIds) || StringUtils.isEmpty(roleId)) {
			reason = "参数错误!";
		} else {
			List<String> list = Arrays.asList(userIds.split(","));
			userGroupRoleMapper.deleteRelationForRole(list, roleId);
			success = true;
		}
		
		json.put("success", success);
		json.put("reason", reason);
		return json;
	}

	@Override
	public Object saveBatchRole(String userGroupId, String roleIds) {
		logger.info("saveBatchRole,userId:{},roleIds:{}",userGroupId,roleIds);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		if (StringUtils.isEmpty(userGroupId) || StringUtils.isEmpty(roleIds)) {
			reason = "参数错误!";
		} else {
			List<String> roleList = new ArrayList<String>(Arrays.asList(roleIds.split(";")));
			for (String roleId : roleList) {
				UserGroupRole relation = new UserGroupRole();
				relation.setUserGroupId(new BigDecimal(userGroupId));
				relation.setRoleId(new BigDecimal(roleId));
				if (userGroupRoleMapper.select(relation).isEmpty()) {
					userGroupRoleMapper.insert(relation);
				}
			}
			success = true;
		}
		json.put("success", success);
		json.put("reason", reason);
		return json;
	}
	
	@Override
	public Object saveBatchUserGroupForRole(String userGroupIds, String roleId) {
		logger.info("saveBatchUserForRole,userIds:{},roleId:{}",userGroupIds,roleId);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		if (StringUtils.isEmpty(userGroupIds) || StringUtils.isEmpty(roleId)) {
			reason = "参数错误!";
		} else {
			List<String> userList = new ArrayList<String>(Arrays.asList(userGroupIds.split(";")));
			for (String userId : userList) {
				UserGroupRole relation = new UserGroupRole();
				relation.setUserGroupId(new BigDecimal(userId));
				relation.setRoleId(new BigDecimal(roleId));
				if (userGroupRoleMapper.select(relation).isEmpty()) {
					userGroupRoleMapper.insert(relation);
				}
			}
			success = true;
		}
		json.put("success", success);
		json.put("reason", reason);
		return json;
	}
}
